/******************* 公共变量 *******************/
var userId = core.getUrlData('user_id'),
	selectedLabel = core.getUrlData('label') ? JSON.parse(decodeURIComponent(core.getUrlData('label'))) : '';

/******************* Vue初始化代码 *******************/
var vm = new Vue({
	el: '.wrap',
	data: {
		userId: userId,
		selectedLabel: (selectedLabel ? selectedLabel : []),
		label: ['旅游', '爬山', '看动漫', '洗盘子', 'cosplay', '玩游戏', '二次元', 'K歌', '电动游戏机', '追剧', '社交交友', 'F1', '黑暗料理', '打麻将', '大胃王', '夜跑', '萌妹子', '搞基', '暖男', '男神', '女神', '瑜伽', '自拍', '健身'],
		selectedNum: '',
		newLabel: '',
		hold: undefined,
		isShow: false
	},
	methods: {
		save: function () {
			var _this = this,
				str = '';
			for (var i = 0; i < this.selectedLabel.length; i++) {
				str += this.selectedLabel[i] + ','
			}
			str = core.subString(str);
			$.ajax({
				url: userUpdateUrl,
				type: 'post',
				data: {
					// user_id: _this.userId,
					label: str
				},
				success: function(data) {
					console.log(data)

					if (data.result == 1) {
						core.hintBack(data.message);
					} else {
						core.layerHint(data.message);
					}
				}
			});
		},
		selectLabel: function (event) {
			var $this = $(event.target);
			if (this.selectedLabel.length == 3) {
				core.layerHint('最多选择3个标签哦~');
			} else {
				var str = $this.html(),
					judge = false;
				for (var i = 0; i < this.selectedLabel.length; i++) {
					if (this.selectedLabel[i] == str) {
						judge = true;
						core.layerHint('不可选择两个相同的标签');
					}
				}
				if (!judge) {
					this.selectedLabel.push(str);
				}
			}
		},
		removeLabel: function () {
			var $this = $(event.target),
				_this = this;
			$.each(this.selectedLabel, function(index, item){
				if (item == $this.html()) {
					_this.selectedLabel.splice($this.index(), 1);
				}
			});
		},
		addNewLabel: function () {
			if (this.newLabel) {
				this.label.push(this.newLabel);
			} else {
				core.layerHint('请输入新标签');
			}
		},
		holdStart: function () {
			this.hold.hold();
		},
		holdEnd: function () {
			this.hold.stop();
		},
		delLabel: function (index) {
			this.label.splice(index, 1);
		}
	},
	computed: {
		selectedNum: function () {
			return this.selectedLabel.length + '/3';
		}
	},
	components: {
		
	},
	created: function () {
		var _this = this;
		this.hold = core.hold({
			time: 500,
			hold: function () {
				_this.isShow = true;
			}
		});
	},
	mounted: function () {
		var _this = this;
		common.bindBack();

		$('body').on('tap', function (event) {
			if (!$(event.target).hasClass('delete-btn')) {
				_this.isShow = false;
			}
		});
	}
});

